package com.financing.wap.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dao.impl.DstUserClientViewDaoImpl;
import com.financing.wap.dto.DstUserClientTagInfoDTO;
import com.financing.wap.dto.DstUserClientViewDTO;
import com.financing.wap.dto.DstUserTagListDTO;
import com.financing.wap.service.ICommonDstService;
import com.financing.wap.service.IDstUserClientTagInfoService;
import com.financing.wap.service.IDstUserClientViewService;
import com.financing.wap.service.IDstUserTagListService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.DstUserTagListVO;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserTagListController")
@RequestMapping("/api/dstUserTagList")
public class DstUserTagListController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserTagListService dstUserTagListService;
	
	@Autowired
	private IDstUserClientTagInfoService dstUserClientTagInfoService;
	
	@Autowired
	private ICommonDstService commonDstService;
	
	@Autowired
	private IDstUserClientViewService dstUserClientViewService;
	
	
	/***
	 * 用户标签列表  -- 当前用户是否包含
	 * @param request
	 * @param dstUserTagList
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listDstUserTagList")
	@ResponseBody
	public ResultMap list(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("query dstUserTagList list begin .......");
			String openid = request.getParameter("openid");
			String clientOpenid = request.getParameter("clientOpenid");
			if(StringUtil.isNullOrEmptyStr(openid,clientOpenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			DstUserTagListVO vo=new DstUserTagListVO();
			vo.setUserOpenid(openid);
			vo.setClientOpenid(clientOpenid);
			List<DstUserTagListVO> list = commonDstService.queryTagByUser(vo);
			result.setAttribute("list",list);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstUserTagList list end .......");
		}catch(Exception e){
			log.error("query dstUserTagList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	/***
	 * 给客户添加标签 
	 * @param request
	 * @param dstUserTagList
	 * @return
	 */
	@RequestMapping("/addDstUserTagList")
	@ResponseBody
	public ResultMap add(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("add dstUserTagList  begin .......");
			String openid = request.getParameter("openid");
			String tagName = request.getParameter("userTagName");
			String clientOpenid = request.getParameter("clientOpenid");
			if(StringUtil.isNullOrEmptyStr(openid,tagName,clientOpenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
		    dstUserTagListService.addTagToClient(openid, tagName, clientOpenid);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstUserTagList list end .......");
		}catch(Exception e){
			log.error("add dstUserTagList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 用户删除标签
	 * @param request
	 * @param dstUserTagList
	 * @return
	 */
	@RequestMapping("/deleteDstUserTagList")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,Integer tagId){
		ResultMap result = new ResultMap();
		try{
			logger.info("delete dstUserTagList  begin .......");
			String openid = request.getParameter("openid");
			if(StringUtil.isNullOrEmptyStr(openid,tagId)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//删除标签
			DstUserTagListDTO dto = dstUserTagListService.loadById(tagId);
			
			if(dto.getId()==null||!openid.equals(dto.getUserOpenid())){
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			dstUserTagListService.deleteTag(openid, tagId);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("delete dstUserTagList  end .......");
		}catch(Exception e){
			log.error("delete dstUserTagList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	/***
	 * 删除指定用户标签
	 * @param request
	 * @param dstUserTagList
	 * @return
	 */
	@RequestMapping("/deleteDstClientTag")
	@ResponseBody
	public ResultMap deleteDstClientTag(HttpServletRequest request,Integer tagId){
		ResultMap result = new ResultMap();
		try{
			logger.info("deleteDstClientTag  begin .......");
			String openid = request.getParameter("openid");
			String clientOpenid = request.getParameter("clientOpenid");
			if(StringUtil.isNullOrEmptyStr(openid,tagId,clientOpenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			DstUserClientTagInfoDTO dstUserClientTagInfo=new DstUserClientTagInfoDTO();
			dstUserClientTagInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserClientTagInfo.setUserClientOpenid(clientOpenid);
			dstUserClientTagInfo.setUserOpenid(openid);
			dstUserClientTagInfo.setClientTagId(tagId);
			DstUserClientTagInfoDTO dto = dstUserClientTagInfoService.queryListGetFirst(dstUserClientTagInfo);
			if(dto.getId()!=null){
				dto.setDeleteTag(CommonStatus.DELETE_TAG_DELETE);
				dstUserClientTagInfoService.update(dto);
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("deleteDstClientTag  end .......");
		}catch(Exception e){
			log.error("deleteDstClientTag error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	/***
	 * 把潜在客户转为客户
	 * @param request
	 * @param dstUserTagList
	 * @return
	 */
	@RequestMapping("/addclient")
	@ResponseBody
	public synchronized ResultMap addclient(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("deleteDstClientTag  begin .......");
			String openid = request.getParameter("openid");
			String clientOpenid = request.getParameter("clientOpenid");
			if(StringUtil.isNullOrEmptyStr(openid,clientOpenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			DstUserClientViewDTO dstUserClientView=new DstUserClientViewDTO();
			dstUserClientView.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserClientView.setUserOpenid(openid);
			dstUserClientView.setUserClientOpenid(clientOpenid);
			int count = dstUserClientViewService.queryListCount(dstUserClientView);
			if(count==0){
				//1为客户
				dstUserClientView.setUserType("1");
				dstUserClientViewService.insert(dstUserClientView);
			}
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("deleteDstClientTag  end .......");
		}catch(Exception e){
			log.error("deleteDstClientTag error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	
}
